<?php

namespace app\admin\controller;


use think\Db;

class Ad extends Authorized
{

    public function index()
    {

       return $this->fetch();
    }

    public function getList()
    {
        $rows = Db::table('ad')->select();
        $rowNums = Db::table('ad')->count();

        return json([
            'code'=>0,
            'msg'=>'',
            'count'=>$rowNums,
            'data'=>$rows,
        ]);
    }

    public function add()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('ad')->insertGetId($post_data);
            if (false === $result) {
                return json(['msg' =>'添加失败', 'code' =>1]);
            } else {
                return json(['msg' =>'添加成功', 'code' =>0]);
            }
        }
       return $this->fetch();
    }

    public function edit()
    {
        $get_data=$this->request->get();
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('ad')->where('id',$post_data['id'])->update($post_data);
            if (false === $result) {
                return json(['msg' =>'修改失败', 'code' =>1]);
            } else {
                return json(['msg' =>'修改成功', 'data'=>Db::table('ad')->getLastSql(),'code' =>0]);
            }
        }
        $info =Db::table('ad')->where('id', $get_data['id'])->find();
        $this->assign('info', $info);
       return $this->fetch();
    }


    public function del()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('ad')->where('id', $post_data['id'])->delete();
            if (false === $result) {
                return json(['msg' =>'删除失败', 'code' =>1]);
            } else {
                return json(['msg' =>'删除成功', 'code' =>0]);
            }
        }
    }
}